package com.shop.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.shop.common.DButils;
import com.shop.entity.goods;
/**
 * 
 * @类名 goodsDaoImpl
 * @描述 TODO
 * @作者： 李健玖
 * @日期：2018年6月30日下午2:17:43
 */
public class goodsDaoImpl implements goodsDaoI {

	@Override
	public boolean insert(goods goods) throws Exception {
		Connection conn=DButils.getConnection();
		String sql="INSERT INTO goods(Gname,Kind,SellPrice,StockPrice,Count,Pname)VALUES(?,?,?,?,?,?)";
		PreparedStatement ps=conn.prepareStatement(sql);
		ps.setString(1,goods.getGname());
		ps.setString(2, goods.getKind());
		if(null!=goods.getSellPrice()){
		ps.setBigDecimal(3, goods.getSellPrice());
		}else{
			ps.setBigDecimal(3, null);
		}
		if(null!=goods.getStockPrice()){
		ps.setBigDecimal(4, goods.getStockPrice());
		}else{
			ps.setBigDecimal(4, null);
		}
		ps.setInt(5, goods.getCount());
		ps.setString(6, goods.getGname());
		if(ps.executeUpdate()>0){
			return true;
		}
		return false;
		
	}

	@Override
	public boolean update(goods goods) throws Exception {
		Connection conn=DButils.getConnection();
		String sql="UPDATE goods SET Gname=?,Kind=?,SellPrice=?,StockPrice=?,Count=?,Pname=? WHERE Goods=?";
		PreparedStatement ps=conn.prepareStatement(sql);
		ps.setString(1,goods.getGname());
		ps.setString(2, goods.getKind());
		if(null!=goods.getSellPrice()){
		ps.setBigDecimal(3, goods.getSellPrice());
		}else{
			ps.setBigDecimal(3, null);
		}
		if(null!=goods.getStockPrice()){
		ps.setBigDecimal(4, goods.getStockPrice());
		}else{
			ps.setBigDecimal(4, null);
		}
		ps.setInt(5, goods.getCount());
		ps.setString(6, goods.getGname());
		ps.setInt(7, goods.getGoods());
		if(ps.executeUpdate()>0){
			return true;
		}
		return false;
		
	}

	@Override
	public boolean delete(int Goods) throws Exception {
		Connection conn=DButils.getConnection();
		String sql="DELETE FROM goods WHERE Goods=?";
		PreparedStatement ps=conn.prepareStatement(sql);
		ps.setInt(1, Goods);
		if(ps.executeUpdate()>0){
			return true;
		}
		return false;
	}

	@Override
	public List<goods> selectAll() throws Exception {
		Connection conn=DButils.getConnection();
		String sql="SELECT * FROM goods ";
		PreparedStatement ps=conn.prepareStatement(sql);
		//将数据库中的记录转换未对象
		List<goods> list=new ArrayList<>();
		//结果集
		ResultSet rs=ps.executeQuery();
		while(rs.next()){
			goods goods=new goods();
			//奖结果集的内容转为对象的属性
			goods.setGoods(rs.getInt(1));
			goods.setGname(rs.getString(2));
			goods.setKind(rs.getString(3));
			goods.setSellPrice(rs.getBigDecimal(4));
			goods.setStockPrice(rs.getBigDecimal(5));
			goods.setCount(rs.getInt(6));
			goods.setPname(rs.getString(7));
			list.add(goods);
		}
		
		return list;
	}

	@Override
	public List<goods> selectWhere(String whereSql) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
